package com.sgb.kjwl.view.widget.packers;

import android.content.Context;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.view.ui.adapter.YoungBaseRecycleAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author younghow
 * @date 2021/6/22
 * @param <T> 参数D代表adapter所用到地参数
 * @param <D> 参数T代表当前使用的adapter
 */
public abstract class WorkTypeSelectSignUpWindow<D extends PickSelectEntity,T extends YoungBaseRecycleAdapter<D>> extends BasePopWindow {

    private T workTypeAdapter;

    private List<D> pickSelectEntityList;

    private String name = "";

    public WorkTypeSelectSignUpWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.laout_work_type_sign_up_window;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        pickSelectEntityList = new ArrayList<>();

        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView cancelView = holder.getPop().findViewById(R.id.cancel_view);

        RecyclerView rvWorkType = holder.getPop().findViewById(R.id.rv_content);

        okView.setOnClickListener(v -> {
            selectResultClick.result(pickSelectEntityList, name);
            dissMiss();
        });
        cancelView.setOnClickListener(v -> dissMiss());

        workTypeAdapter = createAdapter();

        if (workTypeAdapter != null){
            workTypeAdapter.setMultiSelect(true);
            workTypeAdapter.setDefItem(-1);
            rvWorkType.setLayoutManager(new GridLayoutManager(mContext, 4));
            rvWorkType.setAdapter(workTypeAdapter);
            workTypeAdapter.setOnItemMultiClick((pickSelectEntities, position) -> {
                pickSelectEntityList = pickSelectEntities;
                if (pickSelectEntities == null || pickSelectEntities.size() == 0) {
                    name = "";
                } else {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (D pickSelectEntity : pickSelectEntities) {
                        stringBuilder.append(pickSelectEntity.getProjectscale());
                        stringBuilder.append("、");
                    }
                    name = stringBuilder.substring(0, stringBuilder.length() - 1);
                }
            });
        }
    }

    public void setMostSelected(int num) {
        if (workTypeAdapter != null) {
            workTypeAdapter.setMost(num);
        }
    }

    public void setWorkTypeData(List<D> data) {
        workTypeAdapter.setDatas(data);
        workTypeAdapter.setDefItem(-1);
    }

    public void setDefaultItem(int position){
        workTypeAdapter.setDefItem(position);
        workTypeAdapter.setCheckDefault(position);
    }

    public void setDefaultItem(int...position){
        for (int i : position) {
            workTypeAdapter.setDefItem(i);
            workTypeAdapter.setCheckDefault(i);
        }
    }

    public interface SelectResultClick<D> {

        void result(List<D> pickSelectEntities, String name);
    }

    private SelectResultClick<D> selectResultClick;

    public void setSelectResultClick(SelectResultClick<D> selectResultClick) {
        this.selectResultClick = selectResultClick;
    }

    protected abstract T createAdapter();
}
